$(function () {
    bindAll();
});

function bindAll() {
    bindDown_more();
    bindContentImg();
    bindMagnificPopup();
    bindQuestionBtn();
    bindAnswer();
    bindLoadAnswer();
    bindLoadFullAnswer()
    bindAskBtnLink();
}

function bindAnswer(){
    bindResponse();
    bindSupportAnswer()
}

function bindAskBtnLink(){
    $('.ask_question_link').click(function(){
      if(!is_login()){
          toast.error('请先登录！');
          return false;
      }
    })
}

function bindContentImg() {
    $('.popup-gallery').unbind('magnificPopup');
    $('.popup-gallery').each(function () { // the containers for all your galleries
        $(this).magnificPopup({
            delegate: '.popup',
            type: 'image',
            tLoading: 'Loading image #%curr%...',
            mainClass: 'mfp-img-mobile',
            gallery: {
                enabled: true,
                navigateByImgClick: true,
                preload: [0, 1] // Will preload 0 - before current, and 1 after the current image
            },
            image: {
                tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
                titleSrc: function (item) {
                    /*           return item.el.attr('title') + '<small>by Marsel Van Oosten</small>';*/
                    return '';
                }
            }
        });
    });
}

function bindDown_more() {
    $('.down_more').unbind('click');
    $('.down_more').click(function () {
        var id = $(this).attr('data-id');
        var downId = $(this).attr('down-id');
        $.post(U('question/index/getquestion'), {question_id: id}, function (data) {
            if (data.status == 1) {
                $('.down_more_' + downId).html(data.content + '<div class="clearfix"></div>&nbsp;&nbsp;<a data-id="' + id + '" down-id="' + downId + '" class="up_more change" style="color: #f36119">收起 《</a>');
                bindUp_more();
                bindContentImg();
            } else {
                toast.error('请求失败！');
            }
        });
    });
}

function bindUp_more() {
    $('.up_more').unbind('click');
    $('.up_more').click(function () {
        var id = $(this).attr('data-id');
        var downId = $(this).attr('down-id');
        $.post(U('question/index/getquestion'), {question_id: id}, function (data) {
            if (data.status == 1) {
                $('.down_more_' + downId).html(data.content_s + '&nbsp;&nbsp;<a data-id="' + id + '" down-id="' + downId + '" class="down_more change" style="color: #f36119">展开全部内容 》</a>');
                bindDown_more();
                bindContentImg();
            } else {
                toast.error('请求失败！');
            }
        });
    });
}
function bindMagnificPopup() {
    $('.repost-link').unbind('click');
    if (is_login()) {
        $('.open-popup-link').unbind('magnificPopup');
        $('.open-popup-link').magnificPopup({
            type: 'inline',
            midClick: true, // Allow opening popup on middle mouse click. Always set it to true if you don't provide alternative source in href.
            closeOnBgClick: false
        });
    }
    $('.repost-link').click(function () {
        if (is_login()) {
            var id = $.trim($(this).attr('question-id'));
            $.post(U('question/index/getquestion'), {question_id: id}, function (data) {
                if (data.status == 1) {
                    $('#repost_content').val(data.other_content);
                    var value_length = data.other_content.length;
                    var html = value_length + '/' + initQuestionNum;
                    $('.show_num_repost').html(html);
                    $('#question_title').html(data.title);
                    $('#repost_content').focus();
                }
            });
            $('#question-id').val(id);
        } else {
            toast.error('转发前请先登录！');
            return;
        }
    });
}

function bindSupportAnswer(){
    $('.support_answer').unbind('click');
    $('.support_answer').click(function(){
        var answer_id=$(this).attr('answer-id');
        postSupport(answer_id,1);
    });
    $('.oppose_answer').unbind('click');
    $('.oppose_answer').click(function(){
        var answer_id=$(this).attr('answer-id');
        postSupport(answer_id,0);
    });
}

function postSupport(answer_id,type){
    $.post(U('question/index/supportanswer'),{answer_id:answer_id,type:type},function(msg){
        if(msg.status==1){
            toast.success('操作成功');
            $('#answer_support_'+answer_id).html(msg.html);
        }else{
            handleAjax(msg);
        }
    },'json');
}

function bindLoadAnswer(){
    $('.load_answer').unbind('click');
    $('.load_answer').click(function(){
        var question_id=$(this).attr('question-id');
        $.post(U('Question/Index/loadanswer'),{question_id:question_id},function(msg){
            if(msg.status==1){
                $('#question_answer_' + question_id).html(msg.content);
                bindAnswer();
                toast.success('操作成功');
            }else{
                handleAjax(msg);
            }
        },'json');
    });
}

function bindLoadFullAnswer(){
    $('.load_full_answer').unbind('click');
    $('.load_full_answer').click(function(){
        var answer_id=$(this).attr('answer-id');
        $.post(U('Question/Index/loadfullanswer'),{answer_id:answer_id},function(msg){
            if(msg.status==1){
                $('#answer_content_html_' + answer_id).html(msg.content);
                toast.success('操作成功');
            }else{
                handleAjax(msg);
            }
        },'json');
    });
}

function bindQuestionBtn(){
    $('.question_answer_btn').click(function(){
        now_question_id=$(this).attr('question-id');
        displayAnswer();
    });
    $('.question_answer_btn_my').click(function(){
        if(!is_login()){
            toast.error('请先登录!');
            return false;
        }
        now_question_id=$(this).attr('question-id');
        showAnswer();
        $('#answer_content_' + now_question_id).focus();
        $('#to-answer-id-'+now_question_id).val(0);
        $('#to-response-id-'+now_question_id).val(0);
        changeTip('回答问题');
    });
}

function bindResponse(){
    $('.answer_response_btn').unbind('click');
    $('.answer_response_btn').click(function(){
        if(is_login()){
            var answer_id=$(this).attr('answer-id');
            if($('#is-only-answer-'+answer_id).val()){
                now_question_id=answer_id;
                displayAnswer();
            }
            var uid=$(this).attr('user-id');
            var nickname=$(this).attr('user-nickname');
            $('#to-answer-id-'+now_question_id).val(answer_id);
            $('#to-response-id-'+now_question_id).val(0);
            changeTip('评价 <a ucard="'+uid+'">'+nickname+'</a> 的答案');
            ucard();
            $('#answer_content_' + now_question_id).val('');
            $('#answer_content_' + now_question_id).focus();
        }else{
            loginTip();
        }
    });
    $('.response_response_btn').unbind('click');
    $('.response_response_btn').click(function(){
        if(is_login()){
            var answer_id=$(this).attr('answer-id');
            var response_id=$(this).attr('response-id');
            var uid=$(this).attr('user-id');
            var nickname=$(this).attr('user-nickname');
            $('#to-answer-id-'+now_question_id).val(answer_id);
            $('#to-response-id-'+now_question_id).val(response_id);
            changeTip('回复 <a ucard="'+uid+'">'+nickname+'</a> 的评价');
            ucard();
            $('#answer_content_' + now_question_id).val('');
            $('#answer_content_' + now_question_id).focus();
        }else{
            loginTip();
        }
    });
}

function displayAnswer(){
    if($('#question_answer_box_'+now_question_id).is(':visible')){
        hideAnswer();
    }else{
        showAnswer();
    }
}
function hideAnswer(){
    $('#question_answer_box_'+now_question_id).hide();
}
function showAnswer(){
    $('#question_answer_box_'+now_question_id).show();
}
function changeTip(str){
    $('#response-info-'+now_question_id).html(str);
}
function loginTip(){
    toast.info('请先登录');
}
/**问题回答、回复、评论框**/
var now_question_id = 0;
var initAnswerNum;

function appendAnswer() {
    if(is_login()){
        close_face();
        var myDate = new Date();
        var answer_id = $('#to-answer-id-' + now_question_id).val();
        var response_id = $('#to-response-id-' + now_question_id).val();
        var content = $.trim($('#answer_content_' + now_question_id).val());
        var only_answer=$('#is-only-answer-'+now_question_id).val();
        if (content.length < 4) {
            toast.error('内容长度不能小于4！');
            return;
        }
        $.post(U('Question/Index/postanswer'), {question_id: now_question_id, answer_id: answer_id, response_id: response_id, content: content}, function (msg) {
            if (msg.status == 1) {
                if (msg.answer.to_answer_id > 0) {
                    $('#answer_repose_' +answer_id).prepend(msg.answerHtml);
                    toast.success('操作成功');
                    $('#answer_response_num_'+answer_id).html(msg.answer_num);
                } else {
                    $('#question_answer_' + now_question_id).prepend(msg.answerHtml);
                    toast.success('回答成功');
                    $('#answer_num_'+now_question_id).html(msg.question_answer_num);
                }
                bindAnswer();
                $('#answer_content_' + now_question_id).val('');
                $('#answer_content_' + now_question_id).focus();
                if(!only_answer){
                    $('#to-answer-id-'+now_question_id).val(0);
                    $('#to-response-id-'+now_question_id).val(0);
                    changeTip('回答问题');
                }
            } else {
                handleAjax(msg);
            }
        }, 'json');
    }else{
        loginTip();
    }

}
$('.answer_content').keypress(function (e) {
    if (e.ctrlKey && e.which == 13 || e.which == 10) {
        appendAnswer();
    }
});

var ID_setInterval_answer;

function checkNum_answer(obj) {
    var value = obj.val();
    var value_length = value.length;
    var can_in_num = initAnswerNum - value_length;
    if (can_in_num < 0) {
        value = value.substr(0, initAnswerNum);
        obj.val(value);
        value_length = initAnswerNum;
    }
    var html = value_length + '/' + initAnswerNum;
    $('.show_num_answer_' + now_question_id).html(html);
}
function startCheckNum_answer(obj, id) {
    now_question_id = id;
    ID_setInterval_answer = setInterval(function () {
        checkNum_answer(obj);
    }, 250);
}
function endCheckNum_answer() {
    clearInterval(ID_setInterval_answer);
}




